package com.example.administrator.gridview180510;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.content.Context;



public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("网络视图");
        GridView gv=(GridView)findViewById(R.id.grid_view);
        gv.setAdapter(new ImageAdapter(this));
    }

    public class ImageAdapter extends BaseAdapter{
        private Context mContext;

        public ImageAdapter(Context c){
            mContext=c;
        }

        private Integer[] mThumbIds={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10,R.drawable.p11,R.drawable.p12,R.drawable.p13,R.drawable.p14,R.drawable.p15,R.drawable.p16,R.drawable.p17,R.drawable.p18,R.drawable.p19,R.drawable.p20,R.drawable.p21};

        //获取图片的个数
        public int getCount(){
            return mThumbIds.length;
        }

        //获取图片在库中的位置
        public Object getItem(int arg0){
            return null;
        }

        //获取图片的ID
        public long getItemId(int arg0){
            return 0;
        }

        public View getView(int position,View converView,ViewGroup parent){
            ImageView imageView;
            if(converView==null){
                imageView=new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(120,120));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(1,1,1,1);
            }
            else{
                imageView=(ImageView)converView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

    }
}
